package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.Inpstock;
import com.pojo.JdInventory;
import com.service.InpstockService;
import com.service.InventoryService;
import com.vo.QueryVo;
import com.vo.ResultVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

//入库
@Controller
@RequestMapping ("inpstock")
@ResponseBody
public class InpstockController {
    
    @Resource
    private InpstockService inpstockService;
    @Resource
    private InventoryService inventoryService;
    
    @RequestMapping (value = "list", method = RequestMethod.GET)
    public ResultVo<Inpstock> queryByPage(Integer pageNum , Integer pageSize , QueryVo vo) {
        System.out.println("vo : " + vo);
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Inpstock> inpstockPageInfo = inpstockService.queryByPage(pageNum , pageSize , vo);
        System.out.println("inpstockPageInfo : " + inpstockPageInfo);
        return new ResultVo<>(inpstockPageInfo);
    }
    
    @RequestMapping (value = "", method = RequestMethod.POST)
    public ResultVo<Inpstock> addInpstock(Inpstock inpstock) {
        
        //提取出 指定 商品编号、入库数
        Integer in_number = inpstock.getIn_number();
        String p_id = inpstock.getP_id();
        //存信息的更变
        List<JdInventory> inventories = inventoryService.queryByType(new JdInventory(p_id));
        int tmap = 0;
        
        //如果商品库存里没有库存信息，则自动新建列信息
        if(inventories.isEmpty()){
            tmap = addInventory(inpstock , in_number , p_id);
        }else {
            JdInventory inventory = inventories.get(0);
            //所填写的仓库是否相同
            if (inventory.getW_id() == inpstock.getW_id()){
                //如果也有库存信息，则追加数据 入库、库存
                if(inventories.size() == 1){
                    inventory.setI_InpNum(inventory.getI_InpNum()+in_number);
                    inventory.setI_InventoryNum(inventory.getI_InventoryNum()+in_number);
                    tmap = inventoryService.update(inventory);
                }
            }else{
                tmap = addInventory(inpstock , in_number , p_id);
            }
        }
    
        inpstock.setIn_time(new Date());
        int i = inpstockService.addInpstock(inpstock);
        if (i == 0 || tmap == 0) {
            return new ResultVo<>(500 , "服务器异常！");
        }
        return new ResultVo<>();
    }
    
    /**
     * 新建数据
     */
    private int addInventory(Inpstock inpstock , Integer in_number , String p_id) {
        int tmap;
        JdInventory inventory = new JdInventory();
        inventory.setP_id(p_id);
        inventory.setW_id(inpstock.getW_id());
        inventory.setI_InventoryNum(in_number);
        inventory.setI_outNum(0);
        inventory.setI_InpNum(in_number);
        tmap = inventoryService.addInventory(inventory);
        return tmap;
    }
    
    @RequestMapping (value = "{in_num}", method = RequestMethod.DELETE)
    public ResultVo<Inpstock> delectInpstock(@PathVariable ("in_num") Integer in_num) {
        int i = inpstockService.delectInpstock(in_num);
        if (i == 0) {
            return new ResultVo<>(500 , "服务器异常！");
        }
        return new ResultVo<>();
    }
    
    @RequestMapping (value = "{in_num}", method = RequestMethod.GET)
    public ResultVo<Inpstock> queryByKey(@PathVariable ("in_num") Integer in_num) {
        Inpstock inpstock = inpstockService.queryByKey(in_num);
        System.out.println("inpstock : " + inpstock);
        if (inpstock == null) {
            return new ResultVo<>(500 , "服务器异常！");
        }
        return new ResultVo<>(inpstock);
    }
    
    @RequestMapping (value = "" , method = RequestMethod.PUT)
    public ResultVo<Inpstock> update(Inpstock inpstock){
        int i = inpstockService.update(inpstock);
        if (i == 0) {
            return new ResultVo<>(500 , "服务器异常！");
        }
        return new ResultVo<>();
    }
}
